#include "TCPClient.h"
#include "URI.h"

TCPClient::TCPClient(MSIPAgent* agent, InetSocketAddress host, string deviceId)
{
    super(agent);
    hostAddress = host;
    this->deviceId = deviceId;
}

TCPClient::~TCPClient(void)
{
}

TCPClient* TCPClient::create(MSIPAgent *agent, string urlStr, string deviceId)
{
	URI uri;
	if (!uri.parseURI(urlStr)) {
		// Should throw exception here.
		return NULL;
	}

	if (!MSIPStringUtil::equalsIgnoreCase(Protocol::SCHEME, uri.getScheme()) {
		// Should throw exception here.
		return NULL;
	}

	int port = uri.getPort();
	if (port <= 0) {
		port = DEFAULT_PORT;
	}
	InetSocketAddress hostAddr = InetSocketAddress::getByNameAndPort(uri.getHost(), port);
	
    return new TCPClient(agent, hostAddr, deviceId);
}
